﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YJKDataLookAfter;
using YJKRevitFamilyDataBase;

namespace YJKFamilyLibManager.FamilyLibModule
{
	public partial class DlgCommentary : System.Windows.Forms.Form
	{
		public DlgCommentary(DbUser user, DbFamily dbFamily)
		{
			this.mUser = user;
			this.mDbFamily = dbFamily;
			this.InitializeComponent();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.richTextBox1.Text.Trim().Length == 0)
			{
				MessageBox.Show("评论不允许提交空内容。");
				return;
			}
			new DbCommentary(this.mUser, this.richTextBox1.Text, this.mDbFamily).AddToDb(this.mDbFamily.Database);
			MessageBox.Show("评论成功。");
			base.DialogResult = DialogResult.OK;
		}

		private void DlgCommentary_Load(object sender, EventArgs e)
		{
			foreach (DbCommentary dbCommentary in DbCommentary.GetByFamily(this.mDbFamily.Database, this.mDbFamily))
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				dataGridViewRow.CreateCells(this.dataGridView1, new object[]
				{
					dbCommentary.User.Name,
					dbCommentary.Text,
					dbCommentary.Date.ToString(),
					false,
					dbCommentary.Id
				});
				if (!dbCommentary.User.Equals(this.mUser))
				{
					dataGridViewRow.ReadOnly = true;
				}
				this.dataGridView1.Rows.Add(dataGridViewRow);
			}
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.Rows.Count > 0)
			{
				List<DataGridViewRow> list = new List<DataGridViewRow>();
				List<DbCommentary> list2 = new List<DbCommentary>();
				foreach (object obj in ((IEnumerable)this.dataGridView1.Rows))
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					if (dataGridViewRow.Cells["Remove"].Value != null && (bool)dataGridViewRow.Cells["Remove"].Value)
					{
						list.Add(dataGridViewRow);
						DbCommentary dbCommentary = DbCommentary.GetDbCommentary(Convert.ToInt32(dataGridViewRow.Cells["Id"].Value), this.mDbFamily.Database);
						if (dbCommentary != null)
						{
							list2.Add(dbCommentary);
						}
					}
				}
				if (list.Count > 0)
				{
					if (MessageBox.Show("是否将选中的评论删除？", "项目管理器", MessageBoxButtons.YesNo) != DialogResult.Yes)
					{
						return;
					}
					foreach (DataGridViewRow dataGridViewRow2 in list)
					{
						this.dataGridView1.Rows.Remove(dataGridViewRow2);
					}
					using (List<DbCommentary>.Enumerator enumerator3 = list2.GetEnumerator())
					{
						while (enumerator3.MoveNext())
						{
							DbCommentary dbCommentary2 = enumerator3.Current;
							dbCommentary2.RemoveFromDb();
						}
						return;
					}
				}
				MessageBox.Show("没有选中任何评论。");
			}
		}

		private DbUser mUser;

		private DbFamily mDbFamily;
	}
}
